/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.ProjectIndex;
import com.wolfpire.proms.service.ProjectIndexService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.User;

/**
 * 项目指标控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/projectIndex")
public class ProjectIndexController {
	
	@Resource ProjectIndexService projectIndexService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("proms/projectindex/projectindex_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<ProjectIndex> queryList(EasyuiPage<ProjectIndex> page, ProjectIndex filterProjectIndex) {
		Page<ProjectIndex> projectIndexs = this.projectIndexService.setPageDataList(page, filterProjectIndex);
		return new EasyuiPagination<ProjectIndex>(projectIndexs );
	}
	
	@ResponseBody
	@RequestMapping("/projectIndexs")
	public List<ProjectIndex> projectIndexs(ProjectIndex projectIndex) {
		if (null != projectIndex && null != projectIndex.getId()) {
			projectIndex.setParentId(projectIndex.getId());
			projectIndex.setId(null);
		}
		return this.projectIndexService.list(projectIndex);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, ProjectIndex projectIndex) {
		ModelAndView mv = new ModelAndView("proms/projectindex/projectindex_add");
		mv.addObject(projectIndex);
		return mv;
	}
	
	@RequestMapping(value = "/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("proms/projectindex/projectindex_edit");
		if (null != id) {
			ProjectIndex projectIndex = this.projectIndexService.getRoleInfoProjectIndex(id);
			mv.addObject(projectIndex);
		}
		return mv;
	}
	
	@RequestMapping(value = "/detail")
	public ModelAndView detail(Long id) {
		ModelAndView mv = new ModelAndView("proms/projectindex/projectindex_detail");
		if (null != id) {
			ProjectIndex projectIndex = this.projectIndexService.getRoleInfoProjectIndex(id);
			mv.addObject(projectIndex);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Result save(ProjectIndex projectIndex) {
		try {
			this.projectIndexService.saveOrUpdate(projectIndex);
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败，请联系管理员");
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(@RequestParam(value="id")Long id) {
		try {
			this.projectIndexService.del(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
}
